package com.wvmi.gmall.order.config;

import com.wvmi.gmall.order.entity.OrderEntity;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述:  RabbitMQ配置类 <br>
 *
 * @author Wuminming <br>
 * 创建时间: 2023-12-06 14:55 <br>
 * <p>
 * Copyright: 2013 - 2023 www.szwcyq.com Inc. All rights reserved. <br>
 * 注意：本内容仅限于深圳市微创云启科技有限公司内部传阅，禁止外泄以及用于其他的商业目的<br>
 */
@Configuration
public class GmallRabbitMQConfig {

    public final static String ORDER_EVENT_EXCHANGE = "order-event-exchange";
    public final static String ORDER_RELEASE_ORDER_ROUTE_KEY = "order.release.order";
    public final static String ORDER_RELEASE_OTHER_ROUTE_KEY = "order.release.other";
    public final static String ORDER_CREATE_ORDER_ROUTE_KEY = "order.create.order";
    public final static String ORDER_SECKILL_ORDER_ROUTE_KEY = "order.seckill.order";
    public final static String ORDER_SECKILL_QUEUE = "order.seckill.queue";
    public final static String ORDER_DELAY_QUEUE = "order.delay.queue";
    public final static String ORDER_RELEASE_ORDER_QUEUE = "order.release.order.queue";

//    @RabbitListener(queues = ORDER_RELEASE_ORDER_QUEUE)
//    public void listen(Message message, OrderEntity orderEntity){
//        System.out.println("收到消息："+orderEntity.getOrderSn());
//    }

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", ORDER_EVENT_EXCHANGE);
        args.put("x-dead-letter-routing-key", ORDER_RELEASE_ORDER_ROUTE_KEY);
        args.put("x-message-ttl", 60000); // 消息过期时间 1分钟
        /*
            Queue(String name,  队列名字
            boolean durable,  是否持久化
            boolean exclusive,  是否排他
            boolean autoDelete, 是否自动删除
            Map<String, Object> arguments) 属性
         */
        // 死信队列
        return new Queue(ORDER_DELAY_QUEUE,true,false,false,args);
    }
    @Bean
    public Queue orderReleaseOrderQueue(){
        return new Queue(ORDER_RELEASE_ORDER_QUEUE,true,false,false);
    }
    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange(ORDER_EVENT_EXCHANGE,true,false);
    }
    @Bean
    public Binding orderCreateOrderBinding(){
        return new Binding(ORDER_DELAY_QUEUE, Binding.DestinationType.QUEUE,ORDER_EVENT_EXCHANGE,ORDER_CREATE_ORDER_ROUTE_KEY,null);
    }
    @Bean
    public Binding orderReleaseOrderBinding(){
        return new Binding(ORDER_RELEASE_ORDER_QUEUE, Binding.DestinationType.QUEUE,ORDER_EVENT_EXCHANGE, ORDER_RELEASE_ORDER_ROUTE_KEY,null);
    }
    @Bean
    public Binding orderReleaseOtherBinding(){
        return new Binding("stock.release.stock.queue",Binding.DestinationType.QUEUE,ORDER_EVENT_EXCHANGE,ORDER_RELEASE_OTHER_ROUTE_KEY,null);
    }
    @Bean
    public Queue orderSeckillQueue(){
        return new Queue(ORDER_SECKILL_QUEUE,true,false,false);
    }
    @Bean
    public Binding orderSeckillQueueBinding(){
        return new Binding(ORDER_SECKILL_QUEUE, Binding.DestinationType.QUEUE,ORDER_EVENT_EXCHANGE,ORDER_SECKILL_ORDER_ROUTE_KEY,null);
    }
}
